package ro.imc.training.Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ro.imc.training.UMS.Student;

public class StudentCrud implements Crud {

	public StudentCrud(){
		
	}
	
	@Override
	public void insert(Object o) {
		try {
			Connection conn = DriverManager
					.getConnection(DBCreate.DATABASE_URL);

			Student st = (Student) o;

			
			conn.createStatement().execute("insert into student values " +
					"(' "+ st.getLastName() + "','"  +st.getFirstName() + "','" + st.getCNP() +
					"','" + st.getStudentNo() + "','" + st.getStatus()  + " ') ");

			
			
		} catch (SQLException e) {

			e.printStackTrace();
		}

	}

	@Override
	public ArrayList<Object> read() {

		ArrayList<Object> al = new ArrayList<Object>();
		try {
			Connection conn = DriverManager
					.getConnection(DBCreate.DATABASE_URL);
			Statement statement = conn.createStatement();
			ResultSet result = statement.executeQuery("select * from student");
//			ResultSetMetaData rsmd = result.getMetaData();

			while (result.next()) {
				Student s = new Student(result.getString(1),
						result.getString(2), result.getString(3),
						result.getString(4), result.getString(5));
				al.add(s);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return al;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete() {
		// TODO Auto-generated method stub

	}

}
